package com.zhl.media.app.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.muddzdev.styleabletoast.StyleableToast;
import com.orhanobut.hawk.Hawk;
import com.zhl.media.app.R;
import com.zhl.media.app.di.component.DaggerMediaComponent;
import com.zhl.media.app.di.module.MediaModule;
import com.zhl.media.app.event.Event;
import com.zhl.media.app.greendao.AdJustDao;
import com.zhl.media.app.greendao.ElevatorDao;
import com.zhl.media.app.greendao.MediaPhotoDao;
import com.zhl.media.app.greendao.MediaPositionDao;
import com.zhl.media.app.greendao.ProjDao;
import com.zhl.media.app.model.AdJust;
import com.zhl.media.app.model.Elevator;
import com.zhl.media.app.model.MediaPhoto;
import com.zhl.media.app.model.MediaPosition;
import com.zhl.media.app.model.Node;
import com.zhl.media.app.model.Proj;
import com.zhl.media.app.mvp.adapter.MediaGridViewAdapter;
import com.zhl.media.app.mvp.contract.MediaContract;
import com.zhl.media.app.mvp.presenter.MediaPresenter;
import com.zhl.media.app.utils.EntityManager;
import com.zhl.media.commonres.base.AppActivity;
import com.zhl.media.commonres.dialog.BottomDialog;
import com.zhl.media.commonres.dialog.ConfirmDialog;
import com.zhl.media.commonres.dialog.DeleteDialog;
import com.zhl.media.commonres.dialog.InputDialog;
import com.zhl.media.commonres.dialog.WCTZDialog;
import com.zhl.media.commonres.view.ClassifyGridView;
import com.zhl.media.commonres.view.EmptyLayout;
import com.zhl.media.commonres.view.PublicTitleBar;
import com.zhl.media.commonsdk.core.EventBusCode;
import com.zhl.media.commonsdk.core.RouterHub;
import com.zhl.media.commonsdk.entity.AdJustMedia;
import com.zhl.media.commonsdk.enumeration.MediaCode;
import com.zhl.media.commonsdk.utils.CameraUtil;
import com.zhl.media.commonsdk.utils.DeleteFileUtil;

import org.devio.takephoto.app.TakePhoto;
import org.devio.takephoto.app.TakePhotoImpl;
import org.devio.takephoto.compress.CompressConfig;
import org.devio.takephoto.compress.CompressImage;
import org.devio.takephoto.compress.CompressImageImpl;
import org.devio.takephoto.model.InvokeParam;
import org.devio.takephoto.model.TContextWrap;
import org.devio.takephoto.model.TImage;
import org.devio.takephoto.model.TResult;
import org.devio.takephoto.permission.InvokeListener;
import org.devio.takephoto.permission.PermissionManager;
import org.devio.takephoto.permission.TakePhotoInvocationHandler;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

@Route(path = RouterHub.APP_MEDIAACTIVITY)
public class MediaActivity extends AppActivity<MediaPresenter> implements MediaContract.View,TakePhoto.TakeResultListener,InvokeListener {
    @BindView(R.id.titleBar)
    PublicTitleBar titleBar;
    @BindView(R.id.list)
    StickyListHeadersListView list;
    @BindView(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    private MediaPhotoDao mediaPhotoDao;
    private MyAdapter adapter;
    private boolean isExection = false;
    List<Elevator> mData;
    private InvokeParam invokeParam;
    private TakePhoto takePhoto;
    private MediaPosition mediaPosition;
    private Node proj;
    private ElevatorDao elevatorDao;
    private MediaPositionDao mediaPositionDao;
    // 1调整 2补画
    private int tz;
    // 1正常拍照2 重拍
    private int reload;
    private ProjDao projDao;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMediaComponent.builder().appComponent(appComponent).mediaModule(new MediaModule(this)).build().inject(this);
    }
    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_media;
    }
    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        Intent intent = getIntent();
        proj = (Node)intent.getSerializableExtra("proj");
        titleBar.setTitle(proj.getName());
        yc();
        mData = new ArrayList<>();
        adapter = new MyAdapter(this);
        list.setAdapter(adapter);
        initDb();
        loadData();
    }
    private void initDb() {
        mediaPhotoDao = EntityManager.getInstance(this).getMediaPhotoDao();
        elevatorDao = EntityManager.getInstance(this).getElevatorDao();
        mediaPositionDao = EntityManager.getInstance(this).getMediaPositionDao();
        projDao = EntityManager.getInstance(this).getProjDao();
    }
    private void loadData() {
        mData = elevatorDao.queryBuilder().where(ElevatorDao.Properties.UnitId.eq(proj.getId())).list();
        for (int i = 0; i < mData.size(); i++) {
            List<MediaPosition> list = mediaPositionDao.queryBuilder().where(MediaPositionDao.Properties.ElevatorId.eq(mData.get(i).getId())).list();
            mData.get(i).setMedias(list);
        }
        if(mData.size() == 0){
            emptyLayout.bindView(this.list);
            emptyLayout.showEmpty("~暂无媒体位~");
        }else {
            adapter.notifyDataSetChanged();
        }
    }
    private void yc(){
        isExection = false;
        titleBar.setRight("异常处理", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeyc();
            }
        });
    }
    private void closeyc(){
        isExection = true;
        titleBar.setRight("关闭", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yc();
            }
        });
    }
    @Override
    public void showLoading() {
    }
    @Override
    public void hideLoading() {
    }

    //调整结果
    @Override
    public void onYCres() {
        Proj proj = projDao.queryBuilder().where(ProjDao.Properties.Id.eq(this.proj.getProjectId())).unique();
        //调整
        if(tz == 1){
            mediaPosition.setStatus(MediaCode.DTZ);
            mediaPosition.setStu(MediaCode.DTZ.getKey());
            proj.setSttzNum(proj.getSttzNum()+1);
        }
        //补画
        if(tz == 2){
            mediaPosition.setStatus(MediaCode.XBH);
            mediaPosition.setStu(MediaCode.XBH.getKey());
            proj.setSsbhNum(proj.getSsbhNum()+1);
        }
        //取消调整
        if(tz == 3){
            mediaPosition.setStatus(MediaCode.CSZ);
            mediaPosition.setStu(MediaCode.CSZ.getKey());
            proj.setSttzNum(proj.getSttzNum()-1);
        }

        //取消补画
        if(tz == 4){
            mediaPosition.setStatus(MediaCode.CSZ);
            mediaPosition.setStu(MediaCode.CSZ.getKey());
            proj.setSsbhNum(proj.getSsbhNum()-1);
        }

        mediaPositionDao.update(mediaPosition);
        projDao.update(proj);
        loadData();
        adapter.notifyDataSetChanged();
        Event.postUpdateGzrwList();
        Event.postUpdateDscNum();
    }
    @Override
    public void onDtzCount(int count) {
        if(count>0){
            ConfirmDialog confirmDialog = new ConfirmDialog(MediaActivity.this,"调整位置","目前已有异常未上画媒体位，是否要将其调整到本位置？","取消","调整");
            confirmDialog.show();
            confirmDialog.setListener(new ConfirmDialog.HallListener() {
                @Override
                public void confirm() {
                    confirmDialog.dismiss();
                    ARouter.getInstance().build(RouterHub.APP_ADJUSTEDACTIVITY).withSerializable("media",mediaPosition).navigation(MediaActivity.this);
                }
            });
        }else {
            StyleableToast.makeText(MediaActivity.this, "暂无待调整媒体位", Toast.LENGTH_LONG, R.style.mytoast).show();
        }
    }

    @Override
    public void onAdjustMediaInfo(AdJustMedia data, MediaCode code) {
        if(code == MediaCode.WCTZ){
            WCTZDialog wctzDialog = new WCTZDialog(MediaActivity.this,MediaCode.WCTZ.getValue(),"当前媒体位已经调整到如下新位置",
                    data.getProjectName(),
                    data.getUnitName(),
                    data.getBuildingName(),
                    data.getElevatorName(),
                    data.getUnitName()+"/"+data.getElevatorName()+data.getElevatorPosition()+"/"+data.getInstallPosition());
            wctzDialog.show();
            wctzDialog.setListener(new WCTZDialog.HallListener() {
                @Override
                public void i() {
                    AdJustDao adJustDao = EntityManager.getInstance(MediaActivity.this).getAdJustDao();
                    AdJust adJust = adJustDao.queryBuilder().where(AdJustDao.Properties.OldMediaId.eq(mediaPosition.getId())).unique();
                    MediaPosition newMedia = mediaPositionDao.queryBuilder().where(MediaPositionDao.Properties.Id.eq(adJust.getNewMediaId())).unique();
                    if(newMedia.getStu()==MediaCode.YPZ.getKey() || newMedia.getStu()==MediaCode.YWC.getKey()){
                        StyleableToast.makeText(MediaActivity.this, "已上刊媒体位无法撤销", Toast.LENGTH_LONG, R.style.mytoast).show();
                    }else {
                        wctzDialog.dismiss();
                        mPresenter.cancelAdjust(MediaCode.WCTZ,mediaPosition.getId());
                    }
                }
            });
        }
        if(code == MediaCode.YTZ){
            WCTZDialog wctzDialog = new WCTZDialog(MediaActivity.this,MediaCode.YTZ.getValue(),"当前媒体位由如下原位置调整而来",
                    data.getProjectName(),
                    data.getUnitName(),
                    data.getBuildingName(),
                    data.getElevatorName(),
                    data.getUnitName()+"/"+data.getElevatorName()+data.getElevatorPosition()+"/"+data.getInstallPosition());
            wctzDialog.show();
            wctzDialog.setListener(new WCTZDialog.HallListener() {
                @Override
                public void i() {
                    wctzDialog.dismiss();
                    mPresenter.cancelAdjust(MediaCode.YTZ,mediaPosition.getId());
                }
            });
        }
    }

    @Override
    public void cancelAdjust(Map map) {

        String mediaId = (String)map.get("mediaId");
        String unitId = (String)map.get("unitId");
        String buildingId = (String)map.get("buildingId");
        String projectId = (String)map.get("projectId");


        String oldMediaId = (String)map.get("oldMediaId");
        String oldUnitId = (String)map.get("oldUnitId");
        String oldBuildingId = (String)map.get("oldBuildingId");
        String oldProjectId = (String)map.get("oldProjectId");

        //更新新位置
        Proj newPro = projDao.queryBuilder().where(ProjDao.Properties.Id.eq(projectId)).unique();
        newPro.setYtzNum(newPro.getYtzNum()-1);
        newPro.setSumNum(newPro.getSumNum()-1);
        if(newPro.getSumNum()==0){
            newPro.setType(2);
            newPro.setParentId("a");
        }
        projDao.update(newPro);

        //更新新位置楼座
        Proj building = projDao.queryBuilder().where(ProjDao.Properties.Id.eq(buildingId)).unique();
        building.setSumNum(building.getSumNum()-1);
        projDao.update(building);

        //更新新位置单元
        Proj unit = projDao.queryBuilder().where(ProjDao.Properties.Id.eq(unitId)).unique();
        unit.setSumNum(unit.getSumNum()-1);
        projDao.update(unit);


        MediaPosition newMedia = mediaPositionDao.queryBuilder().where(MediaPositionDao.Properties.Id.eq(mediaId)).unique();
        newMedia.setStu(0);
        newMedia.setStatus(null);
        newMedia.setAlphabetIdentification(null);
        newMedia.setSchemeId(null);
        newMedia.setSchemeName(null);
        newMedia.setType(1);
        mediaPositionDao.update(newMedia);

        //更新旧位置
        Proj oldPro = projDao.queryBuilder().where(ProjDao.Properties.Id.eq(oldProjectId)).unique();
        oldPro.setSttzNum(oldPro.getSttzNum()-1);
        oldPro.setYiNum(oldPro.getYiNum()-1);
        projDao.update(oldPro);

        //更新旧位置楼座
        Proj oldBuilding = projDao.queryBuilder().where(ProjDao.Properties.Id.eq(oldBuildingId)).unique();
        oldBuilding.setYiNum(oldBuilding.getYiNum()-1);
        projDao.update(oldBuilding);
        //更新旧位置单元
        Proj oldUnit = projDao.queryBuilder().where(ProjDao.Properties.Id.eq(oldUnitId)).unique();
        oldUnit.setYiNum(oldUnit.getYiNum()-1);
        projDao.update(oldUnit);

        MediaPosition oldMedia = mediaPositionDao.queryBuilder().where(MediaPositionDao.Properties.Id.eq(oldMediaId)).unique();
        oldMedia.setStatus(MediaCode.CSZ);
        oldMedia.setStu(MediaCode.CSZ.getKey());
        mediaPositionDao.update(oldMedia);

        loadData();
        adapter.notifyDataSetChanged();

        Event.postUpdateGzrwList();
        Event.postUpdateDscNum();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Subscriber(tag = "update_mtw")
    public void update_mtw(int code) {
        if(code == EventBusCode.MTW){
            loadData();
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void takeFail(TResult result, String msg) {

    }

    @Override
    public void takeCancel() {

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.TPermissionType type=PermissionManager.onRequestPermissionsResult(requestCode,permissions,grantResults);
        PermissionManager.handlePermissionsResult(this,type,invokeParam,this);
    }

    @Override
    public PermissionManager.TPermissionType invoke(InvokeParam invokeParam) {
        PermissionManager.TPermissionType type=PermissionManager.checkPermission(TContextWrap.of(this),invokeParam.getMethod());
        if(PermissionManager.TPermissionType.WAIT.equals(type)){
            this.invokeParam = invokeParam;
        }
        return type;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        getTakePhoto().onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    public TakePhoto getTakePhoto(){
        if (takePhoto==null){
            takePhoto = (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this,this));
        }
        return takePhoto;
    }

    @Override
    public void takeSuccess(TResult result) {
        List<MediaPhoto> list = mediaPhotoDao.queryBuilder().where(MediaPhotoDao.Properties.MediaId.eq(mediaPosition.getId())).list();
        if(list.size()>0){
            mediaPhotoDao.deleteInTx(list);
        }
        //已完成拍照
        MediaPhoto mediaPhoto = new MediaPhoto(result.getImage().getCompressPath(),1,mediaPosition.getId(),0,0,reload);
        mediaPhotoDao.insert(mediaPhoto);
        Bundle bundle = new Bundle();
        bundle.putSerializable("media",mediaPosition);
        bundle.putString("projId",proj.getProjectId());
        bundle.putString("buildingId",proj.getBuildingId());
        bundle.putString("unitId",proj.getUnitId());
        bundle.putInt("action",1);
        ARouter.getInstance().build(RouterHub.APP_PREVIEWACTIVITY).with(bundle).navigation();
    }



    public class MyAdapter extends BaseAdapter implements StickyListHeadersAdapter {

        private LayoutInflater inflater;

        public MyAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int positions, View convertView, ViewGroup parent) {
            List<MediaPosition> listM = mData.get(positions).getMedias();
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.media_list_item, parent, false);
                holder.gv = (ClassifyGridView) convertView.findViewById(R.id.gv);
                holder.emptyLayout = (EmptyLayout) convertView.findViewById(R.id.emptyLayoutView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if(listM.size() == 0){
                holder.emptyLayout.bindView(holder.gv);
                holder.emptyLayout.setPadding(20,20,20,20);
                holder.emptyLayout.showEmpty("~暂无媒体位~");
            }else {
                holder.emptyLayout.bindView(holder.gv);
                holder.emptyLayout.hideEmpty();
                MediaGridViewAdapter adapter = new MediaGridViewAdapter(MediaActivity.this,R.layout.media_gv_item,listM);
                holder.gv.setAdapter(adapter);
                holder.gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        mediaPosition = listM.get(position);
                        if(!isExection){
                            //正常
                            if(mediaPosition.getType()==3){
                                if(mediaPosition.getStatus() == MediaCode.YPZ){
                                    Bundle bundle = new Bundle();
                                    bundle.putSerializable("media",mediaPosition);
                                    bundle.putString("projId",proj.getProjectId());
                                    bundle.putString("buildingId",proj.getBuildingId());
                                    bundle.putString("unitId",proj.getUnitId());
                                    bundle.putInt("action",2);
                                    ARouter.getInstance().build(RouterHub.APP_PREVIEWACTIVITY).with(bundle).navigation();
                                }else if(mediaPosition.getStatus() == MediaCode.CSZ || mediaPosition.getStatus() == MediaCode.YTZ) {
                                    reload = 1;
                                    CameraUtil.takePhotoCompress(getTakePhoto());
                                }else if(mediaPosition.getStatus() == MediaCode.YWC){
                                    reload = 2;
                                    CameraUtil.takePhotoCompress(getTakePhoto());
                                }  else {
                                    StyleableToast.makeText(MediaActivity.this, "异常媒体位请进行异常处理操作!", Toast.LENGTH_LONG, R.style.mytoast).show();
                                }
                            }
                        }else if(isExection){
                            //异常
                            if(mediaPosition.getType() == 3){
                                if(mediaPosition.getStatus() == MediaCode.CSZ){
                                    List<String> dialogItem = new ArrayList<>();
                                    dialogItem.add("调整位置");
                                    dialogItem.add("需要补画");
                                    BottomDialog dialog = new BottomDialog(MediaActivity.this, dialogItem,"异常处理","该媒体位如果出现了无法上画的情况，可以选择以下选项进行调整操作。");
                                    dialog.setItemClickListener(new BottomDialog.ItemClickListener() {
                                        @Override
                                        public void itemClick(int position, String cotent) {
                                            if(position == 0){
                                                InputDialog inputDialog = new InputDialog(MediaActivity.this,"调整位置","请输入本次操作调整的原因");
                                                inputDialog.show();
                                                inputDialog.setListener(new InputDialog.HallListener() {
                                                    @Override
                                                    public void input(String content) {
                                                        inputDialog.dismiss();
                                                        tz = 1;
                                                        mPresenter.saveAdjust(mediaPosition.getId(),content,1);
                                                    }
                                                });
                                            }
                                            if(position == 1){
                                                InputDialog inputDialog = new InputDialog(MediaActivity.this,"需要补画","请输入本次操作补画的原因");
                                                inputDialog.show();
                                                inputDialog.setListener(new InputDialog.HallListener() {
                                                    @Override
                                                    public void input(String content) {
                                                        inputDialog.dismiss();
                                                        tz = 2;
                                                        mPresenter.saveAdjust(mediaPosition.getId(),content,2);
                                                    }
                                                });
                                            }
                                        }
                                    });
                                    dialog.show();
                                }else if(mediaPosition.getStatus() == MediaCode.DTZ) {
                                    List<String> dialogItem = new ArrayList<>();
                                    dialogItem.add("撤销调整");
                                    BottomDialog dialog = new BottomDialog(MediaActivity.this, dialogItem,"待调整","该媒体位还没有调整到新位置");
                                    dialog.setItemClickListener(new BottomDialog.ItemClickListener() {
                                        @Override
                                        public void itemClick(int position, String item) {
                                            dialog.dismiss();
                                            tz = 3;
                                            mPresenter.updateAdjust(mediaPosition.getId());
                                        }
                                    });
                                    dialog.show();
                                }else if(mediaPosition.getStatus() == MediaCode.XBH) {
                                    List<String> dialogItem = new ArrayList<>();
                                    dialogItem.add("撤销补画");
                                    BottomDialog dialog = new BottomDialog(MediaActivity.this, dialogItem, "需要补画", "该媒体位无法上画，需要重新操作补画");
                                    dialog.setItemClickListener(new BottomDialog.ItemClickListener() {
                                        @Override
                                        public void itemClick(int position, String item) {
                                            dialog.dismiss();
                                            tz = 4;
                                            mPresenter.updateAdjust(mediaPosition.getId());
                                        }
                                    });
                                    dialog.show();
                                }else if(mediaPosition.getStatus() == MediaCode.WCTZ){
                                    mPresenter.adjustMediaInfo(MediaCode.WCTZ,mediaPosition.getId());
                                }
                                else if(mediaPosition.getStatus() == MediaCode.YTZ){
                                    mPresenter.adjustMediaInfo(MediaCode.YTZ,mediaPosition.getId());
                                }
                                else {
                                    StyleableToast.makeText(MediaActivity.this, "此媒体位无法进行异常操作!", Toast.LENGTH_LONG, R.style.mytoast).show();
                                }
                            }else if(mediaPosition.getType() == 1){
                                mPresenter.selectAdjustCount();
                            }
                        }
                    }
                });
                holder.gv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                        if(!isExection){
                            if(listM.get(position).getStatus()==MediaCode.YPZ){
                                DeleteDialog dialog = new DeleteDialog(MediaActivity.this, "您确定删除此结果图片吗？");
                                dialog.show();
                                dialog.setListener(new DeleteDialog.DeleteHallListener() {
                                    @Override
                                    public void delete() {
                                        MediaPhoto unique = mediaPhotoDao.queryBuilder().where(MediaPhotoDao.Properties.MediaId.eq(listM.get(position).getId())).unique();
                                        DeleteFileUtil.deleteFile(unique.getPhotoPath());
                                        mediaPhotoDao.delete(unique);
                                        listM.get(position).setImgUrl(null);
                                        listM.get(position).setStatus(MediaCode.CSZ);
                                        dialog.dismiss();
                                        notifyDataSetChanged();
                                        Event.postUpdateDscNum();
                                        Event.postUpdateDscNum();
                                    }
                                });
                            }
                        }
                        return true;
                    }
                });
            }
            return convertView;
        }
        @Override
        public View getHeaderView(int position, View convertView, ViewGroup parent) {
            HeaderViewHolder holder;
            if (convertView == null) {
                holder = new HeaderViewHolder();
                convertView = inflater.inflate(R.layout.header, parent, false);
                holder.text = (TextView) convertView.findViewById(R.id.tv);
                convertView.setTag(holder);
            } else {
                holder = (HeaderViewHolder) convertView.getTag();
            }
            holder.text.setText(mData.get(position).getName());
            if(mData.get(position).getType()!=1){
                holder.text.setBackgroundColor(Color.parseColor("#00bfff"));
                holder.text.setTextColor(Color.parseColor("#ffffff"));
            }else {
                holder.text.setBackgroundColor(Color.parseColor("#dddddd"));
                holder.text.setTextColor(Color.parseColor("#333333"));
            }
            return convertView;
        }
        @Override
        public long getHeaderId(int position) {
            return Long.valueOf(mData.get(position).getId().hashCode());
        }
        class HeaderViewHolder {
            TextView text;
        }

        class ViewHolder {
            ClassifyGridView gv;
            EmptyLayout emptyLayout;
        }

    }

}

